<template>
  <a-modal
    title="编辑账单"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="賬單類型 （字典 bill_type）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入賬單類型 （字典 bill_type）" v-decorator="['billType', {rules: [{required: true, message: '请输入賬單類型 （字典 bill_type）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="期數"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入期數" v-decorator="['qs', {rules: [{required: true, message: '请输入期數！'}]}]" />
        </a-form-item>
        <a-form-item
          label="收租日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择收租日" v-decorator="['rentedDay',{rules: [{ required: true, message: '请选择收租日！' }]}]" @change="onChangerentedDay"/>
        </a-form-item>
        <a-form-item
          label="租期开始时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期开始时间" v-decorator="['rentStartDate',{rules: [{ required: true, message: '请选择租期开始时间！' }]}]" @change="onChangerentStartDate"/>
        </a-form-item>
        <a-form-item
          label="租期结束时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期结束时间" v-decorator="['rentEndDate',{rules: [{ required: true, message: '请选择租期结束时间！' }]}]" @change="onChangerentEndDate"/>
        </a-form-item>
        <a-form-item
          label="小計"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="實收金額"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="實收金額"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择實收金額" v-decorator="['shouldReceiveDate',{rules: [{ required: true, message: '请选择實收金額！' }]}]" @change="onChangeshouldReceiveDate"/>
        </a-form-item>
        <a-form-item
          label="應收日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择應收日期" v-decorator="['receiveDate',{rules: [{ required: true, message: '请选择應收日期！' }]}]" @change="onChangereceiveDate"/>
        </a-form-item>
        <a-form-item
          label="收款方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收款方式" v-decorator="['collectionType', {rules: [{required: true, message: '请输入收款方式！'}]}]" />
        </a-form-item>
        <a-form-item
          label="收款方式名稱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收款方式名稱" v-decorator="['collectionTypeName', {rules: [{required: true, message: '请输入收款方式名稱！'}]}]" />
        </a-form-item>
        <a-form-item
          label="到账状态（到账，未到账，拖欠,废弃（退租之后），已清算（退租之后））"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入到账状态（到账，未到账，拖欠,废弃（退租之后），已清算（退租之后））" v-decorator="['billStatus', {rules: [{required: true, message: '请输入到账状态（到账，未到账，拖欠,废弃（退租之后），已清算（退租之后））！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { houseBillEdit } from '@/api/modular/main/housebill/houseBillManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        rentedDayDateString: '',
        rentStartDateDateString: '',
        rentEndDateDateString: '',
        shouldReceiveDateDateString: '',
        receiveDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              billType: record.billType,
              qs: record.qs,
              subtotal: record.subtotal,
              receiveAmount: record.receiveAmount,
              collectionType: record.collectionType,
              collectionTypeName: record.collectionTypeName,
              billStatus: record.billStatus
            }
          )
        }, 100)
        // 时间单独处理
        if (record.rentedDay != null) {
            this.form.getFieldDecorator('rentedDay', { initialValue: moment(record.rentedDay, 'YYYY-MM-DD') })
        }
        this.rentedDayDateString = moment(record.rentedDay).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentStartDate != null) {
            this.form.getFieldDecorator('rentStartDate', { initialValue: moment(record.rentStartDate, 'YYYY-MM-DD') })
        }
        this.rentStartDateDateString = moment(record.rentStartDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentEndDate != null) {
            this.form.getFieldDecorator('rentEndDate', { initialValue: moment(record.rentEndDate, 'YYYY-MM-DD') })
        }
        this.rentEndDateDateString = moment(record.rentEndDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.shouldReceiveDate != null) {
            this.form.getFieldDecorator('shouldReceiveDate', { initialValue: moment(record.shouldReceiveDate, 'YYYY-MM-DD') })
        }
        this.shouldReceiveDateDateString = moment(record.shouldReceiveDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.receiveDate != null) {
            this.form.getFieldDecorator('receiveDate', { initialValue: moment(record.receiveDate, 'YYYY-MM-DD') })
        }
        this.receiveDateDateString = moment(record.receiveDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.rentedDay = this.rentedDayDateString
            values.rentStartDate = this.rentStartDateDateString
            values.rentEndDate = this.rentEndDateDateString
            values.shouldReceiveDate = this.shouldReceiveDateDateString
            values.receiveDate = this.receiveDateDateString
            houseBillEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangerentedDay(date, dateString) {
        this.rentedDayDateString = dateString
      },
      onChangerentStartDate(date, dateString) {
        this.rentStartDateDateString = dateString
      },
      onChangerentEndDate(date, dateString) {
        this.rentEndDateDateString = dateString
      },
      onChangeshouldReceiveDate(date, dateString) {
        this.shouldReceiveDateDateString = dateString
      },
      onChangereceiveDate(date, dateString) {
        this.receiveDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
